package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.UserHolder;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.UserLocationApi;
import com.tanhua.db.UserInfo;
import com.tanhua.vo.NearUserVo;
import com.tanhua.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {

    @Reference
    private UserLocationApi userLocationApi;


    public ResponseEntity toAddr(double x_latitude, double y_longitude, String addrStr) {

        userLocationApi.toAddr(x_latitude,y_longitude,addrStr, UserHolder.getUser().getId());

        return ResponseEntity.ok(null);
    }

    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity search(String gender, Integer distance) {

        //根据自己的地理位置 查询附件的人 List<UserLocaltion>
        List<UserLocationVo> voList = userLocationApi.search(gender, distance, UserHolder.getUser().getId());

        ArrayList<NearUserVo> nearUserVoList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(voList)){
            for (UserLocationVo vo : voList) {
                UserInfo userInfo = userInfoApi.findById(vo.getUserId());
                //过滤自己条件
                if(UserHolder.getUser().getId()==userInfo.getId()){
                    continue;
                }
                //过滤性别
                if(!gender.equals(userInfo.getGender())){
                    continue;
                }
                //封装
                NearUserVo nearUserVo = new NearUserVo();
                nearUserVo.setUserId(userInfo.getId());
                nearUserVo.setAvatar(userInfo.getAvatar());
                nearUserVo.setNickname(userInfo.getNickname());

                nearUserVoList.add(nearUserVo);

            }
        }

        return ResponseEntity.ok(nearUserVoList);
    }
}
